﻿Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Windows.Input
Imports System.Windows
Imports System.ComponentModel
Imports OMANE.Modules.TauxGarantis.ViewModels
Imports PTIC_Util
Imports OMANE.Common.Model.Parametrage
Imports Microsoft.Practices.Prism.Commands

Public Class SegmentableValue(Of T)
    Inherits ViewModelBase

    Private commandNotificationMethod As Action(Of Object, Object)
    Private iscomputed As Boolean = False

    Sub New(ByVal commandNotificationMethod As Action(Of Object, Object), _
            ByVal modelName As String, _
            ByVal name As String,
            Optional ByVal iscomputed As Boolean = False)
        _ModelName = modelName
        _Name = name
        Me.commandNotificationMethod = commandNotificationMethod
        AddHandler SegmentProvider.SegmentationChanged, AddressOf SegmentProviderSegmentationChanged
        AddHandler Locator.ModelStatic.ListeParametres(modelName).Requete.EtatCalculDataTableAChange, AddressOf EtatCalculDataTableAChange
        Me.iscomputed = iscomputed
    End Sub
    Sub New()

    End Sub
    Sub New(ByVal commandNotificationMethod As Action(Of Object, Object))
        Me.commandNotificationMethod = commandNotificationMethod
    End Sub

#Region "Name"
    Private _Name As String
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("Name")
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property
#End Region

#Region "Model name"
    Private _ModelName As String
    Public Property ModelName As String
        Get
            Return _ModelName
        End Get
        Set(ByVal value As String)
            OnPropertyChanging("ModelName")
            _ModelName = value
            OnPropertyChanged("ModelName")
        End Set
    End Property
#End Region

#Region "Table des valeurs déclinées"
    Private _Table As DataTable
    Public ReadOnly Property Table As DataTable
        Get
            Return Locator.ModelStatic.ListeParametres(ModelName).Valeurs
        End Get
    End Property
#End Region

#Region "Refresh DataTable"
    Public Sub refreshDataTable(ByVal ModelName As String)
        With SegmentProvider.model.ListeParametres(ModelName)
            If .Valeurs IsNot Nothing Then
                For Each c As DataColumn In .Valeurs.Columns
                    c.ReadOnly = True
                    If c.ColumnName = ModelName Or c.ColumnName = ParametreSelectionMedicale.nomAbattementSelMedi Then
                        c.ReadOnly = False
                    End If
                Next
            End If
            _Table = .Valeurs
        End With
    End Sub
#End Region

#Region "Ajout / Suppression d'un segment"
    Public Sub AddVar(ByVal varname As String)
        'Dim t = If(iscomputed, SegmentProvider.AddVarComputed(ModelName, varname), SegmentProvider.AddVar(ModelName, varname))
        SegmentProvider.AddVar(ModelName, varname)
        refreshDataTable(ModelName)
        OnPropertyChanged("UniqueValue")
        OnPropertyChanged("AvailableVariables")
        OnPropertyChanged("Table")
        OnPropertyChanged("UsedSegmentation")
        OnPropertyChanged("NotUsedSegmentation")
    End Sub
    Public Sub AddVar(ByVal varnames As IEnumerable(Of String))
        'Dim t = If(iscomputed, SegmentProvider.AddVarComputed(ModelName, varnames.ToArray()), SegmentProvider.AddVar(ModelName, varnames.ToArray()))
        SegmentProvider.AddVar(ModelName, varnames.ToArray())
        refreshDataTable(ModelName)
        OnPropertyChanged("UniqueValue")
        OnPropertyChanged("AvailableVariables")
        OnPropertyChanged("Table")
        OnPropertyChanged("UsedSegmentation")
        OnPropertyChanged("NotUsedSegmentation")
    End Sub
    Public Sub RemoveVar(ByVal varname As String)
        'Dim t = If(iscomputed, SegmentProvider.RemoveVarComputed(varname, ModelName), SegmentProvider.RemoveVar(varname, ModelName))
        SegmentProvider.RemoveVar(varname, ModelName)
        refreshDataTable(ModelName)
        OnPropertyChanged("UniqueValue")
        OnPropertyChanged("AvailableVariables")
        OnPropertyChanged("Table")
        OnPropertyChanged("UsedSegmentation")
        OnPropertyChanged("NotUsedSegmentation")
    End Sub
#End Region

#Region "Liste des variables disponibles"
    Public ReadOnly Property AvailableVariables As IEnumerable
        Get
            Return SegmentProvider.GetAvailableVars(ModelName)
        End Get
    End Property
#End Region

#Region "Commandes"
    Public ReadOnly Property EditCommand() As ICommand
        Get
            Return New DelegateCommand(Of Object)(AddressOf ExecuteCommandMethod)
        End Get
    End Property
    Public ReadOnly Property ResetCommand() As ICommand
        Get
            Return New DelegateCommand(Of Object)(AddressOf ExecuteCommandMethod)
        End Get
    End Property
    Public ReadOnly Property LoadCommand() As ICommand
        Get
            Return New DelegateCommand(Of Object)(AddressOf ExecuteCommandMethod)
        End Get
    End Property
    Public ReadOnly Property CopyCommand() As ICommand
        Get
            Return New DelegateCommand(Of Object)(AddressOf ExecuteCommandMethod)
        End Get
    End Property
    Public ReadOnly Property PasteCommand() As ICommand
        Get
            Return New DelegateCommand(Of Object)(AddressOf ExecuteCommandMethod)
        End Get
    End Property

    Private Shared _copiedVars() As String
    Private Sub ExecuteCommandMethod(ByVal parameter As Object)
        'MessageBox.Show(If(parameter Is Nothing, "rrrrr", parameter.ToString()))
        If Not String.IsNullOrEmpty(parameter) Then
            If parameter.ToString().ToLower() = "copy" Then
                Dim segs = Locator.ModelStatic.ListeParametres(ModelName).ListeSegments
                If segs.Count > 0 Then
                    ReDim _copiedVars(segs.Count - 1)
                    segs.CopyTo(_copiedVars, 0)
                End If
            End If
            If parameter.ToString().ToLower() = "paste" Then
                If _copiedVars IsNot Nothing Then
                    If _copiedVars.Length > 0 Then
                        AddVar(_copiedVars)
                        Selected = True
                    End If
                End If
            End If
        End If
        commandNotificationMethod(Me, parameter)
    End Sub
#End Region

#Region "Gestion des valeurs uniques"
    Public Property UniqueValue As T
        Get
            Dim t As GeneriqueTypedParametre(Of T) = SegmentProvider.model.ListeParametres(ModelName)
            Return t.ValeurUnique
        End Get
        Set(ByVal value As T)
            OnPropertyChanging("UniqueValue")
            Dim t As GeneriqueTypedParametre(Of T) = SegmentProvider.model.ListeParametres(ModelName)
            t.ValeurUnique = value
            OnPropertyChanged("UniqueValue")
        End Set
    End Property
#End Region

#Region "Selection"
    Private _Selected As Boolean = False
    Public Property Selected As Boolean
        Get
            Return _Selected
        End Get
        Set(ByVal value As Boolean)
            If _Selected <> value Then
                OnPropertyChanging("Selected")
                _Selected = value
                refreshDataTable(_ModelName)
                OnPropertyChanged("Selected")
            End If
        End Set
    End Property
#End Region

    Public ReadOnly Property UnderlyingType As Type
        Get
            Return GetType(T)
        End Get
    End Property
    Private _NotUsedSegmentation As Boolean = True
    Private _lastTable As DataTable
    Private _lastsegs() As String

    Public Property NotUsedSegmentation As Boolean
        Get
            Return Locator.ModelStatic.ListeParametres(ModelName).Nature = ParamNature.Unique
        End Get
        Set(ByVal value As Boolean)
            If Not value And NotUsedSegmentation Then
                _lastTable = Table.Copy()
                ReDim _lastsegs(Locator.ModelStatic.ListeParametres(ModelName).ListeSegments.Count)
                Locator.ModelStatic.ListeParametres(ModelName).ListeSegments.CopyTo(_lastsegs, 0)


            End If
            OnPropertyChanging("NotUsedSegmentation")
            OnPropertyChanging("UsedSegmentation")
            _NotUsedSegmentation = value
            _UsedSegmentation = Not value
            OnPropertyChanged("UsedSegmentation")
            OnPropertyChanged("NotUsedSegmentation")


        End Set
    End Property
    Private _UsedSegmentation As Boolean = False
    Public Property UsedSegmentation As Boolean
        Get
            Return _UsedSegmentation
        End Get
        Set(ByVal value As Boolean)
            OnPropertyChanging("NotUsedSegmentation")
            OnPropertyChanging("UsedSegmentation")
            _NotUsedSegmentation = Not value
            _UsedSegmentation = value
            OnPropertyChanged("UsedSegmentation")
            OnPropertyChanged("NotUsedSegmentation")
        End Set
    End Property

    Private Sub _table_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)

    End Sub
    Private Sub _table_RowChanging(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)

    End Sub

    Private Sub SegmentProviderSegmentationChanged(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
    Private Sub EtatCalculDataTableAChange(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
End Class

